const app = getApp();
Page({
    data: {
        config: app.config,
        codecard1: "",
        codecard2: "",
        student: "",
        ishow:false,
        comePageSta: '',
        systemSta: 1,
    },
    onLoad(option) {

      my.setNavigationBar({ reset: true });

      this.getSystem();

      let con = option.param;
      this.setData({
        comePageSta: con
      })
        this.get_mrg();
    },

    getSystem(){
      let systemInfo = my.getSystemInfoSync();
      let sta = 1;
      if(systemInfo.platform === "android"){
        sta = 1;
      }else{
        sta = 2;
      }
      this.setData({
        systemSta: sta,
      })
    },

    get_mrg() {
        let user = my.getStorageSync({ key: 'user' }).data;
        console.log(user)
        this.setData({
            user:user,
        })
        if (user.idCardUrl) {
            let arr = JSON.parse(user.idCardUrl)
            if(arr.length ==0) return;
            let app = {
                codecard1:'',
                codecard2:'',
                ishow:true
            }
            arr.forEach((item,index)=>{
                if(item.includes('.UNKNOWN') || item==''){
                    console.log("图片无法识别",item)
                    arr[index] = ''
                    app.ishow = false
                }
            })
            app.codecard1 = arr[0]
            app.codecard2 = arr[1]
            
            this.setData(app)
            console.log("=============>>idcard",arr)
        }
    },

    // 上传图片
    onSubmit() {
        let  res = my.getStorageSync({ key: 'user' }).data;
        console.log(res)
        let arr = [this.data.codecard1, this.data.codecard2];
        let data = {
            idCardUrl: JSON.stringify(arr)
        }
		if (this.data.codecard1 == '') {
			return my.showToast({
					content: '请上传身份证正面', 
				})
		} 
		if (this.data.codecard2 == '') {
			return my.showToast({
					content: '请上传身份证反面', 
				})
		} 
        // return;
        app.user.saveidCard(data)
        .then(json=>{
            console.log(json)
            if (json.code == 200) {
                let user = this.data.user
                user.idCardUrl= JSON.stringify(arr)
                console.log(user.idCardUrl)
                this.setData({
                    user:user,
                    ishow: true
                })
                my.setStorage({
                    key: 'user',
                    data: user,
                })
                my.alert({
                    content: json.msg,
                    success: () => {
                        if(this.data.comePageSta == '"cofirm_come"'){
                          let pages = getCurrentPages();
                          let prevPage = pages[pages.length - 2];
                          prevPage.setData({ 
                            idSta: true
                          })
                        }
                        my.navigateBack();
                    }
                });
            }
        })
    },



    onUpload(e) {
        const name = e.target.dataset.image;
		console.log(name)
        let thisApp = this.data.config.thisName;
        let appUrl = '';
        if(thisApp == 1){
          appUrl = 'https://api.hexinzuji.com';
        }else if(thisApp == 2){
          appUrl = 'https://api.hexinzuji.com';
        }
        my.chooseImage({
            sourceType: ['camera', 'album'],
            count: 1,
            success: (res) => {
                console.log(res)
                const path = res.apFilePaths[0]
                app.other.uploadFile(path,appUrl)
                .then((res)=>{
                    console.log("==============",res)
                    if (JSON.parse(res.data).code==200) {
                        let str = JSON.parse(res.data).data
                        const r = str.split('').reverse().join('');
                        const p = r.search(/\./)
                        str = r.substring(0, p).split('').reverse().join('');
                        console.log(str)
                        if (['jpg', 'jpeg', 'png', 'JPG','JPEG','PNG'].includes(str)) {
                            my.showToast({
                                content: '上传成功',
                            })
                            this.setData({
                                [name]: JSON.parse(res.data).data,
                            })
                        } else if (['UNKNOWN', 'unknown'].includes(str)) {
                            my.showToast({
                                content: '图片格式错误，请重新上传', 
                            })
                        } else {
                            my.showToast({
                                content: '图片无法识别，请重新上传',
                            })
                        }
                        console.log(['jpg', 'jpeg', 'png'].includes(str))
                    } else {
                        my.showToast({
                            content: '图片错误，请重新上传', // 文字内容
                        });
                    }
                })
            },
            fail: (err) => {
              if(err.error != 11){
                my.showToast({
                  content: err.errorMessage
                });
              }
            }
        })
    },
    warningBtn(){
      if(this.data.ishow){
        my.showToast({
            content: '身份证上传后不可修改，如有需要请联系客服'
        });
        return;
      }
      if(!this.data.codecard1){
        my.showToast({
            content: '请上传身份证头像面'
        });
        return;
      }
      if(!this.data.codecard2){
        my.showToast({
            content: '请上传身份证国徽面'
        });
        return;
      }
    }
});
